<?php

namespace app\impl;

use app\interfaces\Task;
use app\common\model\Followtask as TaskModel;
use app\common\model\Answer as AnswerModel;
use app\common\model\PatientInfo as PatientInfoModel;

class SendFollowTask implements Task
{

    public function run(array $data,string $taskId): ?bool
    {
        try {
            $task = (new TaskModel())->getOneData(['id' => $data['id']], 'status');

            if ($task['status'] == 0)
            {
                (new TaskModel())->saveData($data['id'], ['status' => 3]);
                (new PatientInfoModel())->where('establish_followtask_node.task_id', (string)$data['id'])->update(['establish_followtask_node.followStatus' => 3, 'establish_followtask_node.status' => 3]);
            }else{
                (new TaskModel())->saveData($data['id'], $data['data']);
                (new PatientInfoModel())->where('establish_followtask_node.task_id', (string)$data['id'])->update(['establish_followtask_node.followStatus' => 2, 'establish_followtask_node.status' => 2]);
            }
            (new AnswerModel())->where('task_id', $data['id'])->save(['isEnd' => 1]);
            return true;
        } catch (\Exception $exception) {
            \think\facade\Log::record("随访任务结束异常:" . $exception->getMessage(), "error");
        }
        return true;
    }
}